/*
 * 描          述:  <描述>
 * 修  改   人:  Administrator
 * 修改时间:  2016年10月20日
 * <修改描述:>
 */
package com.tx.core.ddlutil.builder.alter.impl;

import com.tx.core.ddlutil.builder.alter.AbstractAlterTableDDLBuilder;
import com.tx.core.ddlutil.builder.alter.AlterTableDDLBuilder;
import com.tx.core.ddlutil.model.TableDef;
import com.tx.core.exceptions.util.AssertUtils;
import com.tx.core.starter.component.DatabaseTypeEnum;

import java.io.IOException;

/**
 * PostgreSQL创建表DDL构建器<br/>
 * <功能详细描述>
 *
 * @author Administrator
 * @version [版本号, 2016年10月20日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class PostgreSQLAlterTableDDLBuilder extends AbstractAlterTableDDLBuilder {

    /**
     * <默认构造函数>
     */
    public PostgreSQLAlterTableDDLBuilder() {
        super();
    }

    /**
     * <默认构造函数>
     */
    public PostgreSQLAlterTableDDLBuilder(TableDef sourceTable) {
        super(sourceTable);
    }

    /**
     * <默认构造函数>
     */
    public PostgreSQLAlterTableDDLBuilder(TableDef newTable, TableDef sourceTable) {
        super(newTable, sourceTable);
    }

    /**
     * @param sourceTable
     * @return
     */
    @Override
    public AlterTableDDLBuilder newInstance(TableDef sourceTable) {
        AssertUtils.notNull(sourceTable, "sourceTable is null.");
        AssertUtils.notEmpty(sourceTable.getName(),
                "sourceTable.tableName is empty.");
        AssertUtils.notEmpty(sourceTable.getColumns(),
                "sourceTable.columns is empty.");

        PostgreSQLAlterTableDDLBuilder builder = new PostgreSQLAlterTableDDLBuilder(
                sourceTable);
        return builder;
    }

    /**
     * @param newTable
     * @param sourceTable
     * @return
     */
    @Override
    public AlterTableDDLBuilder newInstance(TableDef newTable,
                                            TableDef sourceTable) {
        AssertUtils.notNull(newTable, "newTable is null.");
        AssertUtils.notEmpty(newTable.getName(),
                "newTable.tableName is empty.");
        AssertUtils.notNull(sourceTable, "sourceTable is null.");
        AssertUtils.notEmpty(sourceTable.getName(),
                "sourceTable.tableName is empty.");
        AssertUtils.isTrue(newTable.getName()
                        .equalsIgnoreCase(sourceTable.getName()),
                "newTable.tableName:{} should equalsIgnoreCase sourceTable.tableName:{}",
                new Object[]{newTable.getName(),
                        sourceTable.getName()});

        PostgreSQLAlterTableDDLBuilder builder = new PostgreSQLAlterTableDDLBuilder(
                newTable, sourceTable);
        this.columns.addAll(newTable.getColumns());
        this.indexes.addAll(newTable.getIndexes());
        return builder;
    }

    protected void writeAlterDropPrimaryKey() throws IOException {
        print("ALTER TABLE ");
        print(this.tableName);
        print("  DROP CONSTRAINT ");
        println(this.tableName.toLowerCase() + "_pkey;");
    }

    /**
     * 重写删除索引语句
     * @param indexName
     * @throws IOException
     */
    protected void doWriteAlterDropIndex(String indexName) throws IOException {
        //DROP INDEX IDX_TASK_DEF_01 ON task_def;
        print("DROP INDEX ");//输出缩进
        print(indexName);
        println(";");
    }

    @Override
    public DatabaseTypeEnum getDatabaseType() {
        return DatabaseTypeEnum.PostgreSQL;
    }
}
